#include "GCSMap.h"
#include "CPole.h"

//-------------------------------------

void GCSMap::ClearCanvas( QPainter * c, QColor color, int w, int h )
{
    QBrush br = c->brush();
    QPen pn = c->pen();

    c->setBrush( QBrush( color, Qt::SolidPattern ) );
    QPen pp = c->pen();
    pp.setColor( color );
    pp.setStyle( Qt::SolidLine );
    c->setPen( pp );
    c->drawRect( 0, 0, w + 1, h + 1 );

    c->setPen( pn );
    c->setBrush( br );
}

//-------------------------------------

void GCSMap::ClearCanvas()
{
    ClearCanvas( painter, QColor( bgColor ), width() , height() );
}

//-------------------------------------

void GCSMap::DrawFocusRect( const QRect &r )
{
    drawType = dtFocusRectSelect;
    drawingObjs.r = r;
    repaint();
}

//-------------------------------------

void GCSMap::DrawVector( const QPoint & p1, const QPoint & p2 )
{
    drawType = dtVector;
    repaint();
}

//-------------------------------------

void GCSMap::DrawMap()
{
    DrawObjects( false );
    DrawObjectHint();

    switch( drawType )
    {
        case dtFocusRectSelect:
        {
            QBrush br = painter->brush();
            QPen pn = painter->pen();

            painter->setPen( QPen( normColor, 1, Qt::SolidLine ) );
            painter->setBrush( QBrush( selColor, Qt::NoBrush ) );
            painter->drawRect( drawingObjs.r );

            painter->setPen( pn );
            painter->setBrush( br );
        }
        break;

        case dtMoveObject:
        {
            const int dx = ( drawingObjs.absX - mdPntAbs.x() ) ;
            const int dy = ( drawingObjs.absY - mdPntAbs.y() ) ;

            CDrawEngine drawEng( painter, scale, dx-vPortBegin.x(), dy-vPortBegin.y(), viewPort, 1.0f );
            typedef CObjectsMap::ObjMapVector::const_iterator Iter;
            for( Iter i = objMap.Selected().begin(); i!=objMap.Selected().end(); ++i )
            {
                drawEng.SetColor( GetObjColor( i->second ) );
                i->second->Draw( drawEng );
            }
            cursorHint.sprintf( " moving for %d, %d ", int( float( dx ) / scale ), int( float( dy ) / scale ) );

        }
        break;

        case dtPanning:
        {
            DrawObjects(false);
        }
        break;

        case dtVector:
        {
            CDrawEngine de( painter, scale, 0, 0, viewPort, 1.0f );
            de.SetColor( Qt::red );
            de.Line( srcPole->Bounds().center(), mPos );
        }
        break;
    }

    if( pointedObject )
        DrawObjectUnderCursor( painter, pointedObject );

    drawType = dtNone;
}

//-------------------------------------

void GCSMap::DrawObjects( bool repaint )
{
    ClearCanvas();
    ClearObjectsMap();

    if( showGrid )
        DrawGrid();

    if( root )
    {
        MapEntity( root );
        if( !objMap.Empty() )
        {
            CDrawEngine drawEng( painter,
                                 scale,
                                 -vPortBegin.x(),
                                 -vPortBegin.y(),
                                 viewPort,
                                 1.0f,
                                 GCSMap::DotSize );

           /* CDrawEngine mapEng( painter,
                                scale / float(mapScale),
                                -vPortBegin.x() / mapScale,
                                -vPortBegin.y() / mapScale,
                                viewPort,
                                1.0f,
                                GCSMap::DotSize );*/


            typedef CObjectsMap::ObjMapVector::const_iterator Iter;

            QBrush buff_br = painter->brush();
            QPen buff_pn = painter->pen();

            for( Iter i = objMap.NotSelected().begin(); i!=objMap.NotSelected().end(); ++i )
            {
                drawEng.SetColor( GetObjColor( i->second ) );
                i->second->Draw( drawEng );
                //painter->setBrush( QBrush( Qt::NoBrush, Qt::SolidPattern ) );
                //mapEng.SetColor(i->first);
                //i->second->Draw( mapEng );
                painter->setPen( buff_pn );
                painter->setBrush( buff_br );
            }

            QRect dims;
            for( Iter i = objMap.Selected().begin(); i!=objMap.Selected().end(); ++i )
            {
                painter->setBrush( QBrush( Qt::NoBrush, Qt::SolidPattern ) );
                QPen pp = painter->pen();
                pp.setStyle( Qt::DotLine );
                pp.setWidth( 1 );
                painter->setPen( pp );

                drawEng.SetColor( GetObjColor( i->second ) );
                i->second->Draw( drawEng );
                //mapEng.SetColor(i->first);
                //i->second->Draw( mapEng );
                if( IDimentions* idim = dynamic_cast<IDimentions*>(i->second) )
                {
                    drawEng.SetColor( selColor );
                    dims = idim->GetGlobalDims();
                    drawEng.SelectionBox( dims );
                    //mapEng.SelectionBox( dims );
                }
                painter->setPen( buff_pn );
                painter->setBrush( buff_br );
            }
        }
    }
}

//-------------------------------------

void GCSMap::DrawObjectUnderCursor( QPainter * c, IVisual * obj )
{
    QPen p = c->pen();
    QBrush br = c->brush();

    CDrawEngine de( c, scale, -vPortBegin.x(), -vPortBegin.y(), viewPort, 1.0f );
    QColor color = GetObjColor( obj );
    de.SetColor( color );
    int penWidth = std::min( std::max( int(scale), 1 ), 4 );
    QPen pp = p;
    pp.setWidth( std::max( penWidth * 2, 3 ) );
    c->setPen( pp );
    obj->Draw( de );

    CVisualEntity * e = dynamic_cast< CVisualEntity* >( obj );
    if( e )
    {
        CPole * pole = dynamic_cast< CPole* >(e);
        if( pole )
        {
            pp.setColor( poleColor );
            pp.setWidth( 3 );
        }
        else
        {
            pp.setColor( color );
            pp.setWidth( 1 );
            pp.setStyle( Qt::DotLine );
        }
    }

    c->setPen( pp );
    c->setBrush( QBrush( bgColor, Qt::SolidPattern ) );
    obj->Draw( de );

    c->setPen( p );
    c->setBrush( br );
}

//-------------------------------------

void GCSMap::DrawObjectHint()
{
    QPen p = painter->pen();
    QBrush br = painter->brush();

    QPen pp = p;
    pp.setColor( QColor( gridColor ) );
    painter->setPen( pp );
    painter->drawText( mPos, cursorHint );

    painter->setPen( p );
    painter->setBrush( br );
}

//-------------------------------------







